//
//  ViewController.m
//  JKRunloopTest
//
//  Created by 王冲 on 2018/10/14.
//  Copyright © 2018年 JK科技有限公司. All rights reserved.
//

#import "ViewController.h"
#import "ThreadOCViewController.h"
#import "ThreadCViewController.h"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    // 2.1：控制线程生命周期（线程保活）: OC 语言封装的调用
    [self threadCodeOC];
    
    // 2.2：控制线程生命周期（线程保活）: C 语言封装的调用
    //[self threadCodeOC];
    
}

#pragma mark 2.2：控制线程生命周期（线程保活）: C 语言封装的调用
-(void)threadCodeC{
    
    ThreadCViewController *threadCViewController = [ThreadCViewController new];
    [self.navigationController pushViewController:threadCViewController animated:YES];
}

#pragma mark 2.1：控制线程生命周期（线程保活）: OC 语言封装的调用
-(void)threadCodeOC{
    
    ThreadOCViewController *threadOCViewController = [ThreadOCViewController new];
    [self.navigationController pushViewController:threadOCViewController animated:YES];
}



#pragma mark 1：解决NSTimer在滑动时停止工作的问题
-(void)timer{
    
    NSTimer  *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(click) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
    /*
     UITrackingRunLoopMode和NSDefaultRunLoopMode是真正存在的模式
     [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode];
     [[NSRunLoop currentRunLoop] addTimer:timer forMode:UITrackingRunLoopMode];
     NSRunLoopCommonModes:n并不是一个真的模式，它只是一个标记
     timer 能在 _commandModes 数组中存放的模式下工作
     [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
     */
    
}

#pragma mark Timer
-(void)click{
    
    static int a = 0;
    
    NSLog(@"%d",++a);
    
}

#pragma mark 0：RunLoop主与当前的打印
-(void)runlooptest{
    
    NSRunLoop *runloop;
    CFRunLoopRef *runloop2;
    
    NSLog(@"%p %p",[NSRunLoop currentRunLoop],[NSRunLoop mainRunLoop]);
    NSLog(@"%p %p",CFRunLoopGetCurrent(),CFRunLoopGetMain());
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
